<template>
  <div class="aui-card--fill scroll-box card-box">
    <div class="mod-sys__role pane-shell"
         v-if='isMain'>
      <el-form :inline="true"
               :model="dataForm"
               ref="dataForm"
               :rules="dataRule"
               @keyup.enter.native="search"
               @submit.native.prevent="search">
        <el-form-item prop="AL_name">
          <el-input v-model.trim="dataForm.AL_name"
                    :placeholder="$t('role.name')"
                    clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="search">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item class="addBtn">
          <el-button type="primary"
                     plain
                     icon="el-icon-plus"
                     v-if="$hasPermission('system:sysRole:save')"
                     @click="addOrUpdateHandle()">{{ $t('add') }}角色</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger"
                     plain
                     v-if="$hasPermission('system:sysRole:del')"
                     @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading"
                :data="dataList"
                border
                @selection-change="dataListSelectionChangeHandle"
                @sort-change="dataListSortChangeHandle"
                style="width: 100%;">
        <el-table-column type="selection"
                         header-align="center"
                         align="center"
                         width="50"></el-table-column>
        <el-table-column prop="name"
                         :label="$t('role.name')"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="remark"
                         :label="$t('role.remark')"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column prop="createDate"
                         :label="$t('role.createDate')"
                         sortable="custom"
                         header-align="center"
                         align="center"
                         width="180"></el-table-column>
        <el-table-column :label="$t('handle')"
                         fixed="right"
                         header-align="center"
                         align="center"
                         min-width="80">
          <template slot-scope="scope">
            <el-button type="text"
                       size="small"
                       v-if="$hasPermission('system:sysRole:update') && $hasPermission('system:sysRole:getById')"
                       @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button type="text"
                       @click="openPermissions(scope.row.id)"
                       v-if="$hasPermission('system:sysRolePermission:page')"
                       size="small">{{ $t('RolePermissions') }}</el-button>
            <el-button type="text"
                       @click="openMeMnu(scope.row.id)"
                       v-if="$hasPermission('system:sysRoleMenu:sqlPage')"
                       size="small">{{ $t('CharacterMeMnu') }}</el-button>
            <el-button type="text"
                       size="small"
                       v-if="$hasPermission('system:sysRole:del') && scope.row.id !== '363621105374466048'"
                       @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="page"
                     :page-sizes="[10, 20, 50, 100]"
                     :total="total"
                     layout="total, sizes, prev, pager, next"
                     @size-change="pageSizeChangeHandle"
                     @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible"
                     ref="addOrUpdate"
                     @callBack="getDataList"></add-or-update>
    </div>
    <!-- 角色权限管理 -->
    <role-permission ref="rolePermission"
                     @callBack='closeDialog' />
    <!-- 角色菜单管理 -->
    <role-menu ref="roleMenu"
               @callBack='closeDialog' />

    <!-- 角色权限管理 -->
    <role-permission ref="rolePermission"
                     @callBack='closeDialog' />

  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import rolePermission from './role-permission'
import roleMenu from './role-menu'
import AddOrUpdate from './role-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      isMain: true,
      mixinViewModuleOptions: {
        getDataListURL: '/system/sysRole/page',
        getDataListIsPage: true,
        deleteURL: '/system/sysRole',
        deleteIsBatch: true
      },
      dataForm: {
        AL_name: ''
      }
    }
  },
  components: {
    AddOrUpdate,
    rolePermission,
    roleMenu
  },
  computed: {
    dataRule () {
      return {
        AL_name: [
          { required: false, message: this.$t('validate.required'), trigger: 'change' },
          { min: 0, max: 50, message: '长度在 1 到 50 个字符', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 查询
    search () {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.getPageInit()
        }
      })
    },
    closeDialog () {
      this.isMain = true
    },
    // 打开角色权限管理页面
    openPermissions (id) {
      this.isMain = false
      this.openDialog('rolePermission', { id: id })
    },
    // 打开角色菜单管理
    openMeMnu (id) {
      this.isMain = false
      this.openDialog('roleMenu', { id: id })
    }
  }
}
</script>
